<?php
require_once("../data/db_connection.php");
$db = new db();
$user_id = $_GET['user_id'];
header("Content-Type: text/html; charset=utf8");
?>

<html>
<head>我的好友列表和我为接受邀请的列表</head>
<body>
<form>
我的ID：<input type="text" name="user_id" value="<?php echo $user_id;?>"/><br />
</form>
<h1>我的好友列表</h1>
<?php
	$sql3 = "SELECT * FROM pTableRelation WHERE pUserId = " . $user_id . " and pRelationState = 0";
	if ($result = $db->query($sql3)) {
		while ($row = $result->fetch_assoc()) {
			echo "Friend: " . $row['pFriendId'] . '<br />';
		}
	}
?>
<h1>我的未接受邀请</h1>
<form name="show_uninvite_form" action="" method="post" >
<?php 
	$sql3 = "SELECT pUserId FROM pTableRelation WHERE pFriendId = " . $user_id . " and pRelationState = 1";
	if ($result = $db->query($sql3)) {
		while ($row = $result->fetch_assoc()) {
			//echo "<h3>你的好友:" . $row['pUserId'] . "邀请你</h3><br />";	
			echo "<input type='checkbox' name='unacpt_invts[]' value='" . $row['pUserId'] . "'/>你的好友：" . $row['pUserId'] . '邀请你<br />';
		}	
	}
?>
<input type="submit" name="Submit" value="接受邀请" /> 
</form>
<?php 
	$invts = $_POST['unacpt_invts'];
	$invts_count = sizeof($invts);
	$success_count = 0;
	foreach ($invts as $elem) {
		//echo "想邀请您的有" . $elem . "<br />";
		$sql3 = "UPDATE pTableRelation SET pRelationState = 0 WHERE pUserId = " . $user_id . " and pFriendId = " . $elem;
		//echo "1 sql3 = " . $sql3 . "<br />";
		$return_id = $db->update_get_result($sql3);
		if ($return_id > 0) {
			$success_count++;
		}
		$sql3 = "UPDATE pTableRelation SET pRelationState = 0 WHERE pFriendId = " . $user_id . " and pUserId = " . $elem;
		//echo "2 sql3 = " . $sql3 . "<br />";
		$return_id = $db->update_get_result($sql3);
		if ($return_id > 0) {
			$success_count++;	
		}
	}
	if ($invts_count) {
		if ($success_count == $invts_count * 2) {
			echo "<br />邀请成功！";
		} else {
			echo "<br />邀请失败！";	
		}
	}
?>
</body>
</html>